package com.rjhch.hadoopTest;

import org.apache.hadoop.security.UserGroupInformation;
import org.elasticsearch.xpack.sql.jdbc.EsDriver;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class TestElasticsearchDriver {

    public static void main(String[] args) throws Exception {
        Statement stmt = null;
        ResultSet rs = null;
        UserGroupInformation.loginUserFromKeytab("hadoop/hadoop01@HADOOP.COM", "/etc/security/keytab/hadoop.keytab");
        Class.forName("org.elasticsearch.xpack.sql.jdbc.EsDriver");
        Connection conn = DriverManager.getConnection("jdbc:es://http://hadoop01:9200");

        /* 在Phoenix中，如果table name/view name、column name等字符串不加上双引号就会被认为是大写。所以，这里的brand_name要加上双引号  */
        String sql;
        sql = "select * from test_depb_info2";
//        sql = "delete from test_depb_info2 limit 1";   //不支持delete
        PreparedStatement pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();

        while (rs.next()) {
            int columnCount = rs.getMetaData().getColumnCount();
            List<String> data = new ArrayList<>();
            for (int i = 0; i < columnCount; i++) {
                data.add(rs.getString(i + 1));
            }
            System.err.println(data);
        }
        /* 关闭资源*/
        rs.close();
        pstmt.close();
    }
}
